const HtmlWebpackPlugin = require('html-webpack-plugin'); //installed via npm
const webpack = require('webpack'); //to access built-in plugins
const path = require('path');
const ExtractTextPlugin = require('extract-text-webpack-plugin')
const UglifyJSPlugin = require('uglifyjs-webpack-plugin');


process.env.NODE_ENV = "production";
const env = process.env.NODE_ENV;

const config = {

  devtool: "source-map",

  entry: {
    main: './src/index.js',
    lib: ['react', 'react-dom', 'react-router-dom']
  },

  output: {
    filename: '[name].js',
    path: path.resolve(__dirname, 'dist'),
    publicPath: "/bm/"
  },

  module: {
    rules: [
      {
        enforce: "pre",
        test: /\.js$/,
        exclude: /node_modules/,
        loader: "eslint-loader",
        options: {
          rules: {
            "img-has-alt": "off"
          }
        }
      },
      {
        test: /\.js$/,
        exclude: /node_modules/,
        loader: "babel-loader",
      },

      {
        test: /\.css$/,
        use: ExtractTextPlugin.extract({
          use: {
            loader: 'css-loader',
            options: {
              minimize: true || {/* CSSNano Options */ }
            }
          }
        })
      },
      {
        test: /\.(png|jpg|gif|svg|eot|ttf|woff|woff2)$/,
        loader: 'url-loader',
        query: {
          name: 'assets/[name].[ext]',
          limit: 8192
        }
      }

    ]
  },

  plugins: [
    new webpack.LoaderOptionsPlugin({
      // test: /\.xxx$/, // may apply this only for some modules
      options: {
        eslint: {
          configFile: path.join(__dirname, '.eslintrc'),
          useEslintrc: true
        }
      }
    }),

    new webpack.optimize.CommonsChunkPlugin({
      name: 'lib' // Specify the common bundle's name.
    }),
    new HtmlWebpackPlugin({ template: './src/index.html' }),
    new webpack.DefinePlugin({
      'process.env': {
        NODE_ENV: JSON.stringify('production')
      }
    }),
    new UglifyJSPlugin(),
    new ExtractTextPlugin({
      filename: 'bundle.min.css'
    }),

    new webpack.SourceMapDevToolPlugin({
      filename: '[name].js.map',
      exclude: ['lib.js']
    })
  ]
};

module.exports = config;